<template>
  <div class="rating-container">
    <div 
      class="rating-line" 
      ref="ratingLine"
      @click="handleClick"
      @mousedown="handleLineMouseDown"
    >
      <div class="rating-filled" :style="{ width: filledWidth }"></div>
      <div 
        class="rating-dot" 
        :style="{ left: dotPosition }"
        @mousedown="startDrag"
        @touchstart="startDrag"
      >
        <span class="dot-value">{{ currentRating.toFixed(1) }}</span>
      </div>
      <span class="min-value">0</span>
      <span class="max-value">10</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DragRating',
  props: {
    value: {
      type: Number,
      default: 0,
      validator: (value) => value >= 0 && value <= 10
    }
  },
  data() {
    return {
      currentRating: this.value,
      lineWidth: 0,
      isDragging: false,
      isLineMouseDown: false
    }
  },
  computed: {
    filledWidth() {
      return `${(this.currentRating / 10) * 100}%`
    },
    dotPosition() {
      return `${(this.currentRating / 10) * 100}%`
    }
  },
  mounted() {
    this.initLineWidth()
    window.addEventListener('resize', this.handleResize)
    document.addEventListener('mousemove', this.handleDrag)
    document.addEventListener('touchmove', this.handleDrag, { passive: false })
    document.addEventListener('mouseup', this.stopDrag)
    document.addEventListener('touchend', this.stopDrag)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
    document.removeEventListener('mousemove', this.handleDrag)
    document.removeEventListener('touchmove', this.handleDrag)
    document.removeEventListener('mouseup', this.stopDrag)
    document.removeEventListener('touchend', this.stopDrag)
  },
  methods: {
    initLineWidth() {
      this.lineWidth = this.$refs.ratingLine.offsetWidth
    },
    handleResize() {
      this.initLineWidth()
    },
    handleLineMouseDown(e) {
      // 记录鼠标在评分条上按下
      this.isLineMouseDown = true
      // 立即处理一次点击，避免拖动时首次点击无效
      this.handleClick(e)
    },
    handleClick(e) {
      if (this.isDragging) return
      
      const rect = this.$refs.ratingLine.getBoundingClientRect()
      let clientX = e.clientX
      
      // 处理触摸事件
      if (e.type.includes('touch')) {
        clientX = e.touches[0].clientX
      }
      
      const clickPosition = clientX - rect.left
      this.updateRatingFromPosition(clickPosition)
    },
    startDrag(e) {
      e.preventDefault()
      e.stopPropagation()
      this.isDragging = true
    },
    handleDrag(e) {
      if (!this.isDragging && !this.isLineMouseDown) return
      
      e.preventDefault()
      const rect = this.$refs.ratingLine.getBoundingClientRect()
      let clientX
      
      if (e.type.includes('touch')) {
        clientX = e.touches[0].clientX
      } else {
        clientX = e.clientX
      }
      
      const clickPosition = clientX - rect.left
      this.updateRatingFromPosition(clickPosition)
    },
    stopDrag(e) {
      if (this.isDragging) {
        this.isDragging = false
        this.$emit('change', this.currentRating)
      }
      this.isLineMouseDown = false
    },
    updateRatingFromPosition(position) {
      const clickPosition = Math.max(0, Math.min(position, this.lineWidth))
      const rating = (clickPosition / this.lineWidth) * 10
      this.updateRating(rating)
    },
    updateRating(rating) {
      this.currentRating = parseFloat(rating.toFixed(1))
      this.$emit('input', this.currentRating)
    }
  },
  watch: {
    value(newVal) {
      this.currentRating = newVal
    }
  }
}
</script>

<style scoped>
.rating-container {
  display: flex;
  align-items: center;
  padding: 0.8rem 0;
  user-select: none;
}

.rating-line {
  position: relative;
  height: 0.17rem;
  width: 16.72rem;
  background-color: #e0e0e0;
  border-radius: 0.08rem;
  cursor: pointer;
}

.rating-filled {
  position: absolute;
  height: 100%;
  background-color: rgba(41, 141, 255, 1);
  border-radius: 0.08rem;
  transition: width 0.1s ease;
}

.rating-dot {
  position: absolute;
  width: 0.5rem;
  height: 0.5rem;
  background-color: rgba(41, 141, 255, 1);
  box-shadow: 0 0.03rem 0.1rem rgba(0, 0, 0, 0.2);
  border-radius: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  transition: left 0.1s ease;
  cursor: grab;
  touch-action: none;
  z-index: 10;
}

.rating-dot:active {
  cursor: grabbing;
}

.rating-dot .dot-value {
  position: absolute;
  top: -1.5rem;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.8rem;
  color: rgba(41, 141, 255, 1);
  white-space: nowrap;
  min-width: 2rem;
  text-align: center;
}

.min-value, .max-value {
  position: absolute;
  top: -1.5rem;
  font-size: 0.8rem;
  color: rgba(41, 141, 255, 1);
}

.min-value {
  left: 0;
}

.max-value {
  right: 0;
}
</style>